package com.qys.livesMall.open.controller.app;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.framework.view.BaseQuery;
import com.qys.livesMall.open.modal.vo.OpenSysMessageVo;
import com.qys.livesMall.open.service.H2OpenSysMessageReadLogService;
import com.qys.livesMall.open.service.H2OpenSysMessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统信息 前端控制器
 * </p>
 *
 * @author wzy
 * @since 2023-07-11
 */
@Slf4j
@RestController
@RequestMapping("app/openSysMessage")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Tag(name = "系统信息")
public class AppOpenSysMessageController {

    private final H2OpenSysMessageService h2OpenSysMessageService;

    private final H2OpenSysMessageReadLogService h2OpenSysMessageReadLogService;

    @PostMapping("/pageByApp")
    @Operation(summary = "分页查询用户信息列表")
    public R<Page<OpenSysMessageVo>> page(@RequestBody BaseQuery baseQuery){
        if (null==SecurityUtils.getLoginUser()){
            return R.ok();
        }
        return R.ok(h2OpenSysMessageService.querpage(baseQuery));
    }

    @GetMapping(value = "/getSysMessage/{id}")
    @Operation(summary = "查询系统详情信息")
    public R<OpenSysMessageVo> getSysMessage(@PathVariable Long id) {
        OpenSysMessageVo result=h2OpenSysMessageService.getSysMessage(id);
        return R.ok(result);
    }
   /**
    * 消息变更为已读
    * @param id
    * @return
    */
    @PostMapping(value = "/saveRemind/{id}")
    @Operation(summary = "消息变更为已读")
    public R<?> updateRemind(@PathVariable Long id) {
        h2OpenSysMessageReadLogService.updateRemind(id, SecurityUtils.getUserId());
       return R.ok();
    }

    /**
     * 未读消息数量
     * @return
     */
    @GetMapping(value = "/getUnreadNum")
    @Operation(summary = "未读消息数量")
    public R<?> updateRemind() {
        if(SecurityUtils.getLoginUser()==null){
         return R.ok(0);
        }
        Integer result=h2OpenSysMessageReadLogService.getUnreadNum(SecurityUtils.getUserId());
        return R.ok(result);
    }

    /**
     * 消息删除
     * @param id
     * @return
     */
    @PostMapping(value = "/deleteSysMessage/{id}")
    @Operation(summary = "删除")
    public R<?> deleteSysMessage(@PathVariable Long id) {
        h2OpenSysMessageReadLogService.deleteSysMessage(id, SecurityUtils.getUserId());
        return R.ok();
    }


    @PostMapping("/messageBySys")
    @Operation(summary = "分页查询系统信息列表")
    public R<List<OpenSysMessageVo>> messageBySys(){
        return R.ok(h2OpenSysMessageService.messageBySys());
    }
}
